package com.dartsstats;

import com.dartsstats.R;
import com.dartsstats.R.id;
import com.dartsstats.R.layout;
import com.dartsstats.R.menu;
import com.dartsstats.datasource.PlayerDataSource;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class EditIndividualPlayerActivity extends Activity {

	private Player player;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		long playerID = -1;
		if(extras != null) {
			playerID = extras.getLong("PLAYER_ID");
			player = new PlayerDataSource(this).getPlayerByID((int)playerID);
		}
		setContentView(R.layout.activity_edit_individual_player);
		createView();
	}

	private void createView() {
		EditText firstNameEditText = (EditText)findViewById(R.id.playerFirstName);
		firstNameEditText.setText(player.getFirstName());
		EditText lastNameEditText = (EditText) findViewById(R.id.playerLastName);
		lastNameEditText.setText(player.getLastName());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_edit_individual_player, menu);
		return true;
	}
	
	public void updatePlayer(View view) {
		PlayerDataSource playerDataSource = new PlayerDataSource(this);
		EditText firstNameText = (EditText) findViewById(R.id.playerFirstName);
		String playerFirstName = firstNameText.getText().toString();
		EditText lastNameNext = (EditText) findViewById(R.id.playerLastName);
		String playerLastName = lastNameNext.getText().toString();
		player.setFirstName(playerFirstName);
		player.setLastName(playerLastName);
		playerDataSource.updatePlayer(player);
		finish();
		startActivity(getIntent());
	}

}
